package com.wangsb.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: TODO
 * @author: shenbing.wang@hand-china
 * @date: 2021年02月23日 23:17
 */
public class MyInterceptor implements HandlerInterceptor {
    /*
     *@Description:TODO
     *@author:scott
     *@date:2021/2/23 23:34
     *@param handler:封装当前处理方法的信息
     *@Return: boolean 控制后续方法是否执行
     */

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //HandlerMethod handlerMethod = (HandlerMethod) handler;
        //System.out.println("方法执行前执行  -----preHandle "
        //        + "类名：" + handlerMethod.getBean().getClass().getName()
        //        + "方法名：" + handlerMethod.getMethod().getName()
        //        + "参数:" + Arrays.toString(handlerMethod.getMethod().getParameters()) + "");
        return true;
    }
    /*
     *@Description:在请求执行后执行，在试图渲染之前执行，当处理方法出现异常则不会执行
     *@author:wangshenbing
     *@date:2021/2/23 23:41
     *@param response  可以更改response中的信息
     *@param handler  封装当前处理方法的信息
     *@param modelAndView  封装了model和view可以修改
     *@Return:* @return {@link null }
     */

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println("方法执行后执行----视图渲染之前 ---postHandle");

    }
    /*
     *@Description:在试图渲染之后执行，相当于try  catch finally  中的finally
     *@author:wangshenbing
     *@date:2021/2/23 23:45
     *@param null
     *@Return:
     */

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("----视图渲染之后-----afterCompletion");

    }
}
